<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <title>开源智搜</title>
    <style>
        /* 样式保持不变 */
        :root {
            --boost-blue: #0066cc;
            --boost-orange: #3341ff;
            --text-dark: #333;
            --bg-opacity: 0.5;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            min-height: 100vh;
            background-color: #f5f5f5;
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            display: flex;
            justify-content: center;
            padding-top: 0;
        }

        .container {
            width: 800px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            margin: 20px;
        }

        .search-box {
            padding: 30px;
            position: sticky;
            top: 0;
            background: white;
            z-index: 10;
            border-bottom: 2px solid var(--boost-blue);
        }

        .search-form {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }

        #search-input {
            flex: 1;
            padding: 15px 25px;
            border: 2px solid var(--boost-blue);
            border-radius: 30px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        #search-input:focus {
            border-color: var(--boost-orange);
            outline: none;
            box-shadow: 0 0 8px rgba(255, 153, 51, 0.3);
        }

        #search-button {
            padding: 15px 40px;
            background: var(--boost-blue);
            color: white;
            border: none;
            border-radius: 30px;
            cursor: pointer;
            font-weight: bold;
            transition: background 0.3s ease;
        }

        #search-button:hover {
            background: var(--boost-orange);
        }

        .result {
            padding: 0 30px 30px;
        }

        .result-item {
            background: rgba(255, 255, 255, 0.95);
            padding: 20px;
            margin-bottom: 15px;
            border-left: 4px solid var(--boost-blue);
            border-radius: 4px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s ease;
            overflow-wrap: break-word;
            word-wrap: break-word;
            word-break: break-word;
        }

        .result-item:hover {
            transform: translateX(5px);
        }

        .result-item a {
            color: var(--boost-blue);
            font-size: 18px;
            font-weight: 600;
            text-decoration: none;
        }

        .result-item a:hover {
            text-decoration: underline;
            color: var(--boost-orange);
        }

        .result-item p {
            color: var(--text-dark);
            margin: 10px 0;
            line-height: 1.5;
        }

        .result-item .url {
            color: #666;
            font-size: 14px;
            font-family: monospace;
        }

        @media (max-width: 768px) {
            .container {
                width: 95%;
                margin: 10px;
            }

            .search-form {
                flex-direction: column;
            }

            #search-button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header" style="text-align: center;">
            <h1 style="color: var(--boost-blue); margin-bottom: 10px; font-size: 4em;">开源智搜</h1>
        </header>
        <div class="search-box">
            <div class="search-form">
                <input 
                    type="text" 
                    id="search-input"
                    placeholder="输入你想要搜索的内容"
                    autocomplete="off"
                    autofocus
                >
                <button id="search-button" type="button">搜索一下</button>
            </div>
            <header class="header" style="text-align: center;">
                <p style="color: #0b0505;">为开源网站提供智能化搜索服务</p>
            </header>
        </div>
        <div class="result" id="results-container"></div>
    </div>

    <script>
        // 统一搜索触发方式
        $('#search-button').on('click', Search);
        $('#search-input').on('keypress', function(e) {
            if(e.which === 13) Search();
        });

        function Search() {
            const query = $('#search-input').val().trim();
            if(!query) return;

            $('#search-button').prop('disabled', true).text('Searching...');

            $.ajax({
                type: "GET",
                url: "/s?word=" + encodeURIComponent(query),
                success: function(data) {
                    BuildHtml(data);
                },
                error: function(xhr) {
                    console.error("Search error:", xhr.responseText);
                    $('#results-container').html(
                        `<div class="result-item" style="color: #dc3545;">
                            <p>Error fetching results. Please try again later.</p>
                        </div>`
                    );
                },
                complete: function() {
                    $('#search-button').prop('disabled', false).text('Search');
                }
            });
        }

        function BuildHtml(data) {
            if(data == '' || data == null) {
                // 如果搜索结果为空，跳转到 404 页面
                window.location.href = "/404.html";
                return;
            }

            const container = $('#results-container');
            container.empty();

            if(data.length === 0) {
                container.html(
                    `<div class="result-item">
                        <p>No results found. Try different keywords.</p>
                    </div>`
                );
                return;
            }

            data.forEach(elem => {
                const item = $(`
                    <div class="result-item">
                        <a href="${elem.url}" target="_blank">${elem.title}</a>
                        <p>${elem.desc}</p>
                        <div class="url">${elem.url}</div>
                    </div>
                `);
                container.append(item);
            });
        }
    </script>
</body>
</html>